package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;

import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.PutInfoBiz;
import com.egao.orderB.entity.PutInfo;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2021-12-23 16:56:05
 * @version ：1.0
 */
@Repository
public class PutInfoBizImpl extends BaseDao implements PutInfoBiz {

	@Override
	public JsonResult list(PageResult2<T> page, PutInfo putInfo, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<PutInfo> rm = new BeanPropertyRowMapper<PutInfo>(PutInfo.class);
		StringBuffer sql = new StringBuffer("select p.id,p.put_in_storage_id,p.inside_remark");
    sql.append(",p.entrustment_remark,p.name,p.stock_num,p.salesman_id");
		sql.append(",p.salesman,p.examine_id,p.examine,p.customer_service_id");
		sql.append(",p.customer_service,p.oversea_customer_service,p.operater_id,p.operater");
		sql.append(",p.file_name,p.financial_affairs,p.booking_space,p.cabinet_num");
		sql.append(",p.bill_of_lading,p.customer_id,p.customer,p.booking_agent");
		sql.append(",p.dispatch_agent,p.customs_clearance_agent,p.product_code,p.product_num");
		sql.append(",p.trade_terms,p.reconciliation_date,p.booking_space_etd,p.dispatch_date");
		sql.append(",p.payment_path,p.terms_of_service,p.reference_number_edi,p.ctn_mawb");
		sql.append(",p.originating_area,p.arrival_area,p.place_of_receipt,p.origin_sz");
		sql.append(",p.transit_place,p.destination,p.hbl,p.consignee_id");
		sql.append(",p.consignee,p.notifier_id,p.notifier,p.pick_up_agent");
		sql.append(",p.shipping_mark,p.product_dec,p.shipping_address,p.remark");
		sql.append(",p.order_num,p.create_time,p.create_user,p.update_time");
		sql.append(",p.update_user");
    sql.append(",suser.nick_name createName ");
    sql.append(" from chf_put_info p ");
    sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
    sql.append(" where p.deleted=0 ");
		sql.append(" and p.put_in_storage_id=:put_in_storage_id ");
		map.put("put_in_storage_id", putInfo.getPutInStorageId()+"");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and p.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(putInfo)){
      if(Utils.isNotEmpty(putInfo.getSort())&&Utils.isNotEmpty(putInfo.getOrder())){
        sql.append(" order by p."+putInfo.getSort()+" "+putInfo.getOrder());
      } else {
        sql.append(" order by p.create_time desc");
      }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<PutInfo> list(PutInfo putInfo, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<PutInfo> rm = new BeanPropertyRowMapper<PutInfo>(PutInfo.class);
    StringBuffer sql = new StringBuffer("select p.id,p.put_in_storage_id,p.inside_remark");
    sql.append(",p.entrustment_remark,p.name,p.stock_num,p.salesman_id");
		sql.append(",p.salesman,p.examine_id,p.examine,p.customer_service_id");
		sql.append(",p.customer_service,p.oversea_customer_service,p.operater_id,p.operater");
		sql.append(",p.file_name,p.financial_affairs,p.booking_space,p.cabinet_num");
		sql.append(",p.bill_of_lading,p.customer_id,p.customer,p.booking_agent");
		sql.append(",p.dispatch_agent,p.customs_clearance_agent,p.product_code,p.product_num");
		sql.append(",p.trade_terms,p.reconciliation_date,p.booking_space_etd,p.dispatch_date");
		sql.append(",p.payment_path,p.terms_of_service,p.reference_number_edi,p.ctn_mawb");
		sql.append(",p.originating_area,p.arrival_area,p.place_of_receipt,p.origin_sz");
		sql.append(",p.transit_place,p.destination,p.hbl,p.consignee_id");
		sql.append(",p.consignee,p.notifier_id,p.notifier,p.pick_up_agent");
		sql.append(",p.shipping_mark,p.product_dec,p.shipping_address,p.remark");
		sql.append(",p.order_num,p.create_time,p.create_user,p.update_time");
		sql.append(",p.update_user");
    sql.append(",suser.nick_name createName ");
    sql.append(" from chf_put_info p ");
    sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
    sql.append(" where p.deleted=0 ");
		sql.append(" and p.put_in_storage_id=:put_in_storage_id ");
		map.put("put_in_storage_id", putInfo.getPutInStorageId()+"");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and p.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(putInfo)){
      if(Utils.isNotEmpty(putInfo.getSort())&&Utils.isNotEmpty(putInfo.getOrder())){
        sql.append(" order by p."+putInfo.getSort()+" "+putInfo.getOrder());
      } else {
        sql.append(" order by p.create_time desc");
      }
    }
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public PutInfo getById(PutInfo putInfo) {
		RowMapper<PutInfo> rm = new BeanPropertyRowMapper<PutInfo>(PutInfo.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select p.id,p.put_in_storage_id,p.inside_remark");
    sql.append(",p.entrustment_remark,p.name,p.stock_num,p.salesman_id");
		sql.append(",p.salesman,p.examine_id,p.examine,p.customer_service_id");
		sql.append(",p.customer_service,p.oversea_customer_service,p.operater_id,p.operater");
		sql.append(",p.file_name,p.financial_affairs,p.booking_space,p.cabinet_num");
		sql.append(",p.bill_of_lading,p.customer_id,p.customer,p.booking_agent");
		sql.append(",p.dispatch_agent,p.customs_clearance_agent,p.product_code,p.product_num");
		sql.append(",p.trade_terms,p.reconciliation_date,p.booking_space_etd,p.dispatch_date");
		sql.append(",p.payment_path,p.terms_of_service,p.reference_number_edi,p.ctn_mawb");
		sql.append(",p.originating_area,p.arrival_area,p.place_of_receipt,p.origin_sz");
		sql.append(",p.transit_place,p.destination,p.hbl,p.consignee_id");
		sql.append(",p.consignee,p.notifier_id,p.notifier,p.pick_up_agent");
		sql.append(",p.shipping_mark,p.product_dec,p.shipping_address,p.remark");
		sql.append(",p.order_num,p.create_time,p.create_user,p.update_time");
		sql.append(",p.update_user");
    sql.append(",suser.nick_name createName ");
    sql.append(" from chf_put_info p ");
    sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
    sql.append(" where p.deleted=0 and p.id=:id ");
		map.put("id", putInfo.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public PutInfo getByPutStorageId(String putStorageId) {
		RowMapper<PutInfo> rm = new BeanPropertyRowMapper<PutInfo>(PutInfo.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select p.id,p.put_in_storage_id,p.inside_remark");
    sql.append(",p.entrustment_remark,p.name,p.stock_num,p.salesman_id");
		sql.append(",p.salesman,p.examine_id,p.examine,p.customer_service_id");
		sql.append(",p.customer_service,p.oversea_customer_service,p.operater_id,p.operater");
		sql.append(",p.file_name,p.financial_affairs,p.booking_space,p.cabinet_num");
		sql.append(",p.bill_of_lading,p.customer_id,p.customer,p.booking_agent");
		sql.append(",p.dispatch_agent,p.customs_clearance_agent,p.product_code,p.product_num");
		sql.append(",p.trade_terms,p.reconciliation_date,p.booking_space_etd,p.dispatch_date");
		sql.append(",p.payment_path,p.terms_of_service,p.reference_number_edi,p.ctn_mawb");
		sql.append(",p.originating_area,p.arrival_area,p.place_of_receipt,p.origin_sz");
		sql.append(",p.transit_place,p.destination,p.hbl,p.consignee_id");
		sql.append(",p.consignee,p.notifier_id,p.notifier,p.pick_up_agent");
		sql.append(",p.shipping_mark,p.product_dec,p.shipping_address,p.remark");
		sql.append(",p.order_num,p.create_time,p.create_user,p.update_time");
		sql.append(",p.update_user");
    sql.append(",suser.nick_name createName ");
    sql.append(" from chf_put_info p ");
    sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
    sql.append(" where p.deleted=0 and p.put_in_storage_id=:put_in_storage_id ");
		map.put("put_in_storage_id", putStorageId);
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(PutInfo putInfo, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_put_info (");
    sql.append("put_in_storage_id,inside_remark,entrustment_remark,name");
		sql.append(",stock_num,salesman_id,salesman,examine_id");
		sql.append(",examine,customer_service_id,customer_service,oversea_customer_service");
		sql.append(",operater_id,operater,file_name,financial_affairs");
		sql.append(",booking_space,cabinet_num,bill_of_lading,customer_id");
		sql.append(",customer,booking_agent,dispatch_agent,customs_clearance_agent");
		sql.append(",product_code,product_num,trade_terms,reconciliation_date");
		sql.append(",booking_space_etd,dispatch_date,payment_path,terms_of_service");
		sql.append(",reference_number_edi,ctn_mawb,originating_area,arrival_area");
		sql.append(",place_of_receipt,origin_sz,transit_place,destination");
		sql.append(",hbl,consignee_id,consignee,notifier_id");
		sql.append(",notifier,pick_up_agent,shipping_mark,product_dec");
		sql.append(",shipping_address,remark,order_num,create_user,create_time) values ( ");
    sql.append(":put_in_storage_id,:inside_remark,:entrustment_remark,:name");
		sql.append(",:stock_num,:salesman_id,:salesman,:examine_id");
		sql.append(",:examine,:customer_service_id,:customer_service,:oversea_customer_service");
		sql.append(",:operater_id,:operater,:file_name,:financial_affairs");
		sql.append(",:booking_space,:cabinet_num,:bill_of_lading,:customer_id");
		sql.append(",:customer,:booking_agent,:dispatch_agent,:customs_clearance_agent");
		sql.append(",:product_code,:product_num,:trade_terms,:reconciliation_date");
		sql.append(",:booking_space_etd,:dispatch_date,:payment_path,:terms_of_service");
		sql.append(",:reference_number_edi,:ctn_mawb,:originating_area,:arrival_area");
		sql.append(",:place_of_receipt,:origin_sz,:transit_place,:destination");
		sql.append(",:hbl,:consignee_id,:consignee,:notifier_id");
		sql.append(",:notifier,:pick_up_agent,:shipping_mark,:product_dec");
		sql.append(",:shipping_address,:remark,:order_num,:create_user,now())");
    map.put("put_in_storage_id", putInfo.getPutInStorageId()==null?null:putInfo.getPutInStorageId()+"");
    map.put("inside_remark", putInfo.getInsideRemark());
    map.put("entrustment_remark", putInfo.getEntrustmentRemark());
    map.put("name", putInfo.getName());
    map.put("stock_num", putInfo.getStockNum());
    map.put("salesman_id", putInfo.getSalesmanId()==null?null:putInfo.getSalesmanId()+"");
    map.put("salesman", putInfo.getSalesman());
    map.put("examine_id", putInfo.getExamineId()==null?null:putInfo.getExamineId()+"");
    map.put("examine", putInfo.getExamine());
    map.put("customer_service_id", putInfo.getCustomerServiceId()==null?null:putInfo.getCustomerServiceId()+"");
    map.put("customer_service", putInfo.getCustomerService());
    map.put("oversea_customer_service", putInfo.getOverseaCustomerService());
    map.put("operater_id", putInfo.getOperaterId()==null?null:putInfo.getOperaterId()+"");
    map.put("operater", putInfo.getOperater());
    map.put("file_name", putInfo.getFileName());
    map.put("financial_affairs", putInfo.getFinancialAffairs());
    map.put("booking_space", putInfo.getBookingSpace());
    map.put("cabinet_num", putInfo.getCabinetNum());
    map.put("bill_of_lading", putInfo.getBillOfLading());
    map.put("customer_id", putInfo.getCustomerId()==null?null:putInfo.getCustomerId()+"");
    map.put("customer", putInfo.getCustomer());
    map.put("booking_agent", putInfo.getBookingAgent());
    map.put("dispatch_agent", putInfo.getDispatchAgent());
    map.put("customs_clearance_agent", putInfo.getCustomsClearanceAgent());
    map.put("product_code", putInfo.getProductCode());
    map.put("product_num", putInfo.getProductNum());
    map.put("trade_terms", putInfo.getTradeTerms());
    map.put("reconciliation_date", putInfo.getReconciliationDate());
    map.put("booking_space_etd", putInfo.getBookingSpaceEtd());
    map.put("dispatch_date", putInfo.getDispatchDate());
    map.put("payment_path", putInfo.getPaymentPath());
    map.put("terms_of_service", putInfo.getTermsOfService());
    map.put("reference_number_edi", putInfo.getReferenceNumberEdi());
    map.put("ctn_mawb", putInfo.getCtnMawb());
    map.put("originating_area", putInfo.getOriginatingArea());
    map.put("arrival_area", putInfo.getArrivalArea());
    map.put("place_of_receipt", putInfo.getPlaceOfReceipt());
    map.put("origin_sz", putInfo.getOriginSz());
    map.put("transit_place", putInfo.getTransitPlace());
    map.put("destination", putInfo.getDestination());
    map.put("hbl", putInfo.getHbl());
    map.put("consignee_id", putInfo.getConsigneeId()==null?null:putInfo.getConsigneeId()+"");
    map.put("consignee", putInfo.getConsignee());
    map.put("notifier_id", putInfo.getNotifierId()==null?null:putInfo.getNotifierId()+"");
    map.put("notifier", putInfo.getNotifier());
    map.put("pick_up_agent", putInfo.getPickUpAgent());
    map.put("shipping_mark", putInfo.getShippingMark());
    map.put("product_dec", putInfo.getProductDec());
    map.put("shipping_address", putInfo.getShippingAddress());
    map.put("remark", putInfo.getRemark());
    map.put("order_num", putInfo.getOrderNum()==null?null:putInfo.getOrderNum()+"");
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(PutInfo putInfo, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_put_info set ");
    sql.append("inside_remark=:inside_remark,entrustment_remark=:entrustment_remark ");
		sql.append(",name=:name,stock_num=:stock_num,salesman_id=:salesman_id ");
		sql.append(",salesman=:salesman,examine_id=:examine_id,examine=:examine ");
		sql.append(",customer_service_id=:customer_service_id,customer_service=:customer_service,oversea_customer_service=:oversea_customer_service ");
		sql.append(",operater_id=:operater_id,operater=:operater,file_name=:file_name ");
		sql.append(",financial_affairs=:financial_affairs,booking_space=:booking_space,cabinet_num=:cabinet_num ");
		sql.append(",bill_of_lading=:bill_of_lading,customer_id=:customer_id,customer=:customer ");
		sql.append(",booking_agent=:booking_agent,dispatch_agent=:dispatch_agent,customs_clearance_agent=:customs_clearance_agent ");
		sql.append(",product_code=:product_code,product_num=:product_num,trade_terms=:trade_terms ");
		sql.append(",reconciliation_date=:reconciliation_date,booking_space_etd=:booking_space_etd,dispatch_date=:dispatch_date ");
		sql.append(",payment_path=:payment_path,terms_of_service=:terms_of_service,reference_number_edi=:reference_number_edi ");
		sql.append(",ctn_mawb=:ctn_mawb,originating_area=:originating_area,arrival_area=:arrival_area ");
		sql.append(",place_of_receipt=:place_of_receipt,origin_sz=:origin_sz,transit_place=:transit_place ");
		sql.append(",destination=:destination,hbl=:hbl,consignee_id=:consignee_id ");
		sql.append(",consignee=:consignee,notifier_id=:notifier_id,notifier=:notifier ");
		sql.append(",pick_up_agent=:pick_up_agent,shipping_mark=:shipping_mark,product_dec=:product_dec ");
		sql.append(",shipping_address=:shipping_address,remark=:remark,order_num=:order_num ");
		sql.append(",update_time=now(),update_user=:update_user where id=:id ");
    map.put("inside_remark", putInfo.getInsideRemark());
    map.put("entrustment_remark", putInfo.getEntrustmentRemark());
    map.put("name", putInfo.getName());
    map.put("stock_num", putInfo.getStockNum());
    map.put("salesman_id", putInfo.getSalesmanId()==null?null:putInfo.getSalesmanId()+"");
    map.put("salesman", putInfo.getSalesman());
    map.put("examine_id", putInfo.getExamineId()==null?null:putInfo.getExamineId()+"");
    map.put("examine", putInfo.getExamine());
    map.put("customer_service_id", putInfo.getCustomerServiceId()==null?null:putInfo.getCustomerServiceId()+"");
    map.put("customer_service", putInfo.getCustomerService());
    map.put("oversea_customer_service", putInfo.getOverseaCustomerService());
    map.put("operater_id", putInfo.getOperaterId()==null?null:putInfo.getOperaterId()+"");
    map.put("operater", putInfo.getOperater());
    map.put("file_name", putInfo.getFileName());
    map.put("financial_affairs", putInfo.getFinancialAffairs());
    map.put("booking_space", putInfo.getBookingSpace());
    map.put("cabinet_num", putInfo.getCabinetNum());
    map.put("bill_of_lading", putInfo.getBillOfLading());
    map.put("customer_id", putInfo.getCustomerId()==null?null:putInfo.getCustomerId()+"");
    map.put("customer", putInfo.getCustomer());
    map.put("booking_agent", putInfo.getBookingAgent());
    map.put("dispatch_agent", putInfo.getDispatchAgent());
    map.put("customs_clearance_agent", putInfo.getCustomsClearanceAgent());
    map.put("product_code", putInfo.getProductCode());
    map.put("product_num", putInfo.getProductNum());
    map.put("trade_terms", putInfo.getTradeTerms());
    map.put("reconciliation_date", putInfo.getReconciliationDate());
    map.put("booking_space_etd", putInfo.getBookingSpaceEtd());
    map.put("dispatch_date", putInfo.getDispatchDate());
    map.put("payment_path", putInfo.getPaymentPath());
    map.put("terms_of_service", putInfo.getTermsOfService());
    map.put("reference_number_edi", putInfo.getReferenceNumberEdi());
    map.put("ctn_mawb", putInfo.getCtnMawb());
    map.put("originating_area", putInfo.getOriginatingArea());
    map.put("arrival_area", putInfo.getArrivalArea());
    map.put("place_of_receipt", putInfo.getPlaceOfReceipt());
    map.put("origin_sz", putInfo.getOriginSz());
    map.put("transit_place", putInfo.getTransitPlace());
    map.put("destination", putInfo.getDestination());
    map.put("hbl", putInfo.getHbl());
    map.put("consignee_id", putInfo.getConsigneeId()==null?null:putInfo.getConsigneeId()+"");
    map.put("consignee", putInfo.getConsignee());
    map.put("notifier_id", putInfo.getNotifierId()==null?null:putInfo.getNotifierId()+"");
    map.put("notifier", putInfo.getNotifier());
    map.put("pick_up_agent", putInfo.getPickUpAgent());
    map.put("shipping_mark", putInfo.getShippingMark());
    map.put("product_dec", putInfo.getProductDec());
    map.put("shipping_address", putInfo.getShippingAddress());
    map.put("remark", putInfo.getRemark());
    map.put("order_num", putInfo.getOrderNum()==null?null:putInfo.getOrderNum()+"");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", putInfo.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(PutInfo putInfo, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(putInfo.getId().length()>0){
			for(String ID : putInfo.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_put_info set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
